﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.SoldierExam.Model;
using Yipusoft.SoldierExam.BLL;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;

public partial class Aptitude_StandManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
            if (userInfo.DepartmentID.Substring(0, 4) == "1300")
            {
                ddlDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList("(SUBSTRING(DepartmentID, 5, 3) = '000') OR (DepartmentID = '1313100') OR (DepartmentID = '1312100')");
            }
            else
            {
                ddlDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList("(DepartmentID='" + userInfo.DepartmentID.Substring(0, 4) + "000') ");

            }

            ddlDepartment.DataTextField = "FullName";

            ddlDepartment.DataValueField = "DepartmentID";
            ddlDepartment.DataBind();

            if (userInfo.DepartmentID.Substring(0, 4) == "1300")
                ddlRanger.DataSource = AptitudeRanger.GetRanger();
            else
                ddlRanger.DataSource = AptitudeRanger.GetRangerByTeam(1);
            ddlRanger.DataTextField = "Ranger";
            ddlRanger.DataValueField = "RangerID";
            ddlRanger.DataBind();

            ddlExam.DataSource = AptitudeExam.getList();
            ddlExam.DataTextField = "Subject";
            ddlExam.DataValueField = "ExamID";
            ddlExam.DataBind();

        }
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        gvStandard.PageIndex = PageIndex;
        bindgv();
    }
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        #region 权限
        bool isPermission = false;
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300300"))
        {
            isPermission = true;
        }
        else
        {
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300301"))
            {
                isPermission = true;
            }
        }
        if (!isPermission)
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
            return;
        }
        #endregion
        gvStandard.PageIndex = 0;
        ListPageContral1.SetFirstPage();
        bindgv();
    }

    private void bindgv()
    {
        bool Contranct = false;
        if (rbtContract.SelectedValue == "0")
        {
            Contranct = false;
        }
        else
        {
            Contranct = true;
        }
        List<AptitudeStandardInfo> list = AptitudeStandard.GetList(ddlDepartment.SelectedValue, int.Parse(ddlRanger.SelectedValue), int.Parse(ddlExam.SelectedValue), Contranct);
        ListPageContral1.ItemCount = list.Count;
        gvStandard.DataSource = list;
        gvStandard.DataBind();
    }

    public string GetFullDepartmentName(string soldierID)
    {
        Soldier soldier = new Soldier();

        return soldier.GetSoldierInfoByID(soldierID).DepartmentName;
    }

    public string GetSoldierName(string soldierID)
    {
        Soldier soldier = new Soldier();

        return soldier.GetSoldierInfoByID(soldierID).SoldierName;
    }

    public string GetRangerName(int rangerID)
    {
        return AptitudeRanger.GetRangerById(rangerID).Ranger;
    }

    public string GetStandard(int standard)
    {
        string ret = "";
        switch (standard)
        {
            case -1:
                ret = "不合格";
                break;
            case 0:
                break;
            case 1:
                ret = "合格";
                break;
        }
        return ret;
    }
    //protected void gvStandard_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    AptitudeExam.DeleteStandard(int.Parse(gvStandard.DataKeys[e.RowIndex].Value.ToString()));
    //    gvStandard.DataSource = AptitudeExam.GetStandard(ddlDepartment.SelectedValue, int.Parse(ddlRanger.SelectedValue), int.Parse(ddlExam.SelectedValue));
    //    gvStandard.DataBind();
    //}
    protected void gvStandard_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblID = e.Row.FindControl("lblID") as Label;
            lblID.Text = Convert.ToString(gvStandard.PageSize * ListPageContral1.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");


            //笔试成绩
            AptitudeStandardInfo info = e.Row.DataItem as AptitudeStandardInfo;
            Label lblBiShi = e.Row.FindControl("lblBiShi") as Label;

            lblBiShi.Text = AptitudeExamScore.GetAptitudeExamScore(info.SoldierID.ToString(), info.RangerID, info.ExamID, 13) == null ? "" : AptitudeExamScore.GetAptitudeExamScore(info.SoldierID.ToString(), info.RangerID, info.ExamID, 13) == null ? "" : AptitudeExamScore.GetAptitudeExamScore(info.SoldierID.ToString(), info.RangerID, info.ExamID, 13) == null ? "" : AptitudeExamScore.GetAptitudeExamScore(info.SoldierID.ToString(), info.RangerID, info.ExamID, 13).Score.ToString("0.00");

        }
    }
    protected void gvStandard_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            #region 权限
            bool isPermission = false;
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300300"))
            {
                isPermission = true;
            }
            else
            {
                if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300310"))
                {
                    isPermission = true;
                }
            }
            if (!isPermission)
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
            #endregion
            AptitudeStandard.DeleteStandard(Convert.ToInt32(e.CommandArgument));
            AptitudeExam.DeleteAptitudeExam(Convert.ToInt32(e.CommandArgument));
            EventLogInfo log = new EventLogInfo();
            log.EventTime = DateTime.Now;
            log.EventType = 1;
            log.EventData = "删除了一条资质考核题目";
            log.ItemID = e.CommandArgument.ToString();
            log.ItemName = "Combat";
            log.ItemFullUrl = "Aptitude/InsertExam.aspx";
            log.CreatedBy = Context.User.Identity.Name;
            log.CreatedTime = DateTime.Now;
            log.UserIP = Request.UserHostAddress;
            EventLog.Insert(log);
            Prompt.Text = "提示：共删除1条记录！";
            bool Contranct = false;
            if (rbtContract.SelectedValue == "0")
            {
                Contranct = false;
            }
            else
            {
                Contranct = true;
            }
            gvStandard.DataSource = AptitudeStandard.GetList(ddlDepartment.SelectedValue, int.Parse(ddlRanger.SelectedValue), int.Parse(ddlExam.SelectedValue), Contranct);
            gvStandard.DataBind();
        }
    }
}
